﻿using System;
using System.Linq;
using NuGet.Enterprise.Server.Models;
using System.Data.Services.Common;

namespace NuGet.Enterprise.Server.DataServices
{
    [DataServiceKey("Id", "Version")]
    [EntityPropertyMapping("Id", SyndicationItemProperty.Title, SyndicationTextContentKind.Plaintext, keepInContent: false)]
    [EntityPropertyMapping("Authors", SyndicationItemProperty.AuthorName, SyndicationTextContentKind.Plaintext, keepInContent: false)]
    [EntityPropertyMapping("LastUpdated", SyndicationItemProperty.Updated, SyndicationTextContentKind.Plaintext, keepInContent: false)]
    [EntityPropertyMapping("Summary", SyndicationItemProperty.Summary, SyndicationTextContentKind.Plaintext, keepInContent: false)]
    [HasStream]
    public class PackagesEntity
    {
        #region Statics

        private static string ConvertDependency(PackageDependency dependency)
        {
            return String.Format("{0}:{1}", dependency.Id, dependency.VersionSpec);
        }

        #endregion

        #region Fields
        
        private readonly IServerPackage _package;

        #endregion

        #region Constructors
        
        public PackagesEntity(IServerPackage package)
        {
            _package = package;
        }

        #endregion

        #region Properties

        public string Id
        {
            get { return _package.Id;  }
        }

        public string Version
        {
            get { return _package.Version.ToString(); }
        }

        public string Title
        {
            get { return _package.Title; }
        }

        public string Authors
        {
            get { return String.Join(",", _package.Authors); }
        }

        public string IconUrl
        {
            get { return (_package.IconUrl == null ? null : _package.IconUrl.GetComponents(UriComponents.HttpRequestUrl, UriFormat.Unescaped)); }
        }

        public string LicenseUrl
        {
            get { return (_package.LicenseUrl == null ? null : _package.LicenseUrl.GetComponents(UriComponents.HttpRequestUrl, UriFormat.Unescaped)); }
        }

        public string ProjectUrl
        {
            get { return (_package.ProjectUrl == null ? null : _package.ProjectUrl.GetComponents(UriComponents.HttpRequestUrl, UriFormat.Unescaped)); }
        }

        public string ReportAbuseUrl
        {
            get { return (_package.ReportAbuseUrl == null ? null : _package.ReportAbuseUrl.GetComponents(UriComponents.HttpRequestUrl, UriFormat.Unescaped)); }
        }

        public int DownloadCount
        {
            get { return _package.DownloadCount; }
        }

        public int RatingsCount
        {
            get { return _package.RatingsCount; }
        }

        public double Rating
        {
            get { return _package.Rating; }
        }

        public bool RequireLicenseAcceptance
        {
            get { return _package.RequireLicenseAcceptance; }
        }

        public string Description
        {
            get { return _package.Description; }
        }

        public string Summary
        {
            get { return _package.Summary; }
        }

        public string Language
        {
            get { return _package.Language; }
        }

        public DateTime Published
        {
            get { return _package.Published; }
        }

        public DateTime LastUpdated
        {
            get { return _package.LastUpdated; }
        }

        public decimal Price
        {
            get { return 0; }
        }

        public string Dependencies
        {
            get { return String.Join("|", from d in _package.Dependencies select ConvertDependency(d)); }
        }

        public string PackageHash
        {
            get { return _package.PackageHash; }
        }

        public long PackageSize
        {
            get { return _package.PackageSize; }
        }

        public string ExternalPackageUri
        {
            get { return null; }
        }

        public string Categories
        {
            get { return null; }
        }

        public string Copyright
        {
            get { return null; }
        }

        public string PackageType
        {
            get { return null; }
        }

        public string Tags
        {
            get { return null; }
        }

        #endregion
    }
}